package com.zaaach.citypickerdemo;

import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.HotCity;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

public class MainAbility extends FractionAbility implements AbsButton.CheckedStateChangedListener {

    private ArrayList<HotCity> hotCities;
    private boolean enable;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        EventRunner eventRunner = EventRunner.getMainEventRunner();
        EventHandler eventHandler = new EventHandler(eventRunner);

        Text tv = (Text) findComponentById(ResourceTable.Id_tv);
        Checkbox cb_hot = (Checkbox) findComponentById(ResourceTable.Id_cb_hot);
        Checkbox cb_enable_anim = (Checkbox) findComponentById(ResourceTable.Id_cb_enable_anim);
        cb_hot.setCheckedStateChangedListener(this);
        cb_enable_anim.setCheckedStateChangedListener(this);
        findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CityPicker.getInstance()
                        .setLocatedCity(null)
                        .setHotCities(hotCities)
                        .enableAnimation(enable)
                        .setOnPickListener(new OnPickListener() {
                            @Override
                            public void onPick(int position, City data) {
                                tv.setText("当前城市:" + data.getName() + ", " + data.getCode());
                            }

                            @Override
                            public void onLocate() {
                                eventHandler.postTask(new Runnable() {
                                    @Override
                                    public void run() {
                                        CityPicker.getInstance()
                                                .locateComplete(new LocatedCity("深圳", "广东", "101280601"), LocateState.SUCCESS);
                                    }
                                }, 2000);
                            }

                            @Override
                            public void onCancel() {
                                tv.setText("取消");
                            }
                        })
                        .show(getContext());
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean b) {
        switch (absButton.getId()) {
            case ResourceTable.Id_cb_hot:
                if (b){
                    hotCities = new ArrayList<>();
                    hotCities.add(new HotCity("北京", "北京", "101010100"));
                    hotCities.add(new HotCity("上海", "上海", "101020100"));
                    hotCities.add(new HotCity("广州", "广东", "101280101"));
                    hotCities.add(new HotCity("深圳", "广东", "101280601"));
                    hotCities.add(new HotCity("杭州", "浙江", "101210101"));
                }else {
                    hotCities = null;
                }
                break;
            case ResourceTable.Id_cb_enable_anim:
                enable = b;
                break;
        }
    }
}
